﻿using System;
using System.Collections.Generic;
using System.Linq;
using LoA.Shared.Interfaces;

namespace LoA.Shared.Input {
	/// <summary>
	/// Represents a set of available moves for matching. This internal storage of this
	/// class is optimized for efficient match searches.
	/// </summary>
	public class MoveSequenceCollection {
		public MoveSequence[ ] moves;

		public MoveSequenceCollection(IEnumerable<MoveSequence> moves) {
			// Store the list of moves in order of decreasing sequence length.
			// This greatly simplifies the logic of the DetectMove method.
			this.moves = moves.OrderByDescending(m => m.Sequence.Length).ToArray( );
		}

		/// <summary>
		/// Finds the longest Move which matches the given input, if any.
		/// </summary>
		public MoveSequence DetectMove(IInputManager input) {
			// Perform a linear search for a move which matches the input. This relies
			// on the moves array being in order of decreasing sequence length.
			foreach (MoveSequence move in moves) {
				if (input.Matches(move)) {
					return move;
				}
			}
			return null;
		}

		public int LongestMoveLength {
			get {
				// Since they are in decreasing order,
				// the first move is the longest.
				return moves[0].Sequence.Length;
			}
		}
	}
}
